package com.graduation.fq.teacher.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.graduation.fq.teacher.R;
import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.logic.InformationLogic;
import com.graduation.fq.teacher.module.CourseInfo;
import com.graduation.fq.teacher.module.EventArgs;
import com.graduation.fq.teacher.module.InformationEventArgs;
import com.graduation.fq.teacher.utils.Const;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jifang
 * @version 2016/5/23.
 */
public class CheckCourseActivity extends Activity {
    private List<CourseInfo> courseInfoList;
    private CourseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_course_information);
        String userId = getUserId();
        ListView courseListView = (ListView) findViewById(R.id.course_list);
        initListView(courseListView);

        courseListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CourseInfo info = (CourseInfo) parent.getAdapter().getItem(position);
                Intent intent = new Intent(CheckCourseActivity.this, StudentListActivity.class);
                intent.putExtra("c_id", info.getId());
                startActivity(intent);
            }
        });
        getCourseList(userId);
    }

    private void getCourseList(String userId) {
        InformationLogic logic = InformationLogic.getInstance();
        logic.getCourseList(userId, new EventListener() {
            @Override
            public void onEvent(EventArgs args) {
                InformationEventArgs args1 = (InformationEventArgs) args;
                int status = args.getStatus();
                if (status == Const.HTTP_SUCCESS) {
                    courseInfoList.clear();
                    courseInfoList.addAll(((InformationEventArgs) args).getCourseInfoList());
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void initListView(ListView courseListView) {
        courseInfoList = new ArrayList<>();
        adapter = new CourseAdapter();
        adapter.setCourseInfoList(courseInfoList);
        courseListView.setAdapter(adapter);
    }

    private String getUserId() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String id = preferences.getString("user_id", null);
        return id;
    }

    private class CourseAdapter extends BaseAdapter {
        private List<CourseInfo> courseInfoList;

        public List<CourseInfo> getCourseInfoList() {
            return courseInfoList;
        }

        public void setCourseInfoList(List<CourseInfo> courseInfoList) {
            this.courseInfoList = courseInfoList;
        }

        @Override
        public int getCount() {
            return courseInfoList.size();
        }

        @Override
        public Object getItem(int position) {
            return courseInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CourseInfo info = courseInfoList.get(position);
            CourseHolder holder;
            if (convertView == null) {
                holder = new CourseHolder();
                convertView = getLayoutInflater().inflate(R.layout.layout_course_item, null);
                holder.name = (TextView) convertView.findViewById(R.id.course_item_name);
                holder.credit = (TextView) convertView.findViewById(R.id.course_item_credit);
                convertView.setTag(holder);
            } else {
                holder = (CourseHolder) convertView.getTag();
            }
            holder.name.setText(info.getName());
            holder.credit.setText(String.valueOf(info.getCredit()));
            return convertView;
        }
    }

    private class CourseHolder {
        TextView name;
        TextView credit;
    }
}
